﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using OnlineManagement.Model;
namespace OnlineManagement.Web.WebServices
{
    /// <summary>
    /// Summary description for PropagateService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class PropagateService : System.Web.Services.WebService
    {
        [WebMethod]
        public JsonMsg SowPropagateAdd(int hoggeryId, Guid sowId, DateTime propagateDate, 
            Guid pigTypeId, int healthCount, int weakCount, int deathCount,
            float nestWeight, float maxChildWeight, float minChildWeight, int keepChildTotal,string comment)
        {
            JsonMsg msg = null;
            //满足常识
            if ((healthCount >= 0 && healthCount <= 50) &&
                (weakCount >= 0 && weakCount <= 50) &&
                (deathCount >= 0 && deathCount <= 50) &&
                (nestWeight >= 0 && nestWeight <= 100) &&
                (maxChildWeight >= 0 && maxChildWeight <= 10) &&
                (minChildWeight >= 0 && minChildWeight <= 10) &&
                (keepChildTotal >= 0 && keepChildTotal <= weakCount + healthCount)&&
                (DateTime.Now-propagateDate).Days>=0 )
            {
                Model.PropagateRecord record = new PropagateRecord()
                {
                    HoggeryID = hoggeryId,
                    SowID = sowId,
                    PropagateDate = propagateDate,
                    ChildPigTypeId = pigTypeId,
                    HealthCount = healthCount,
                    WeakCount = weakCount,
                    DeathCount = deathCount,
                    NestWeight = nestWeight,
                    MaxChildWeight = maxChildWeight,
                    MinChildWeight = minChildWeight,
                    KeepChildTotal = keepChildTotal,
                    Comment = comment
                };
                string message;
                bool result = SubmitData(record, out message);
                msg = new JsonMsg() { Flag=result,Message=message };
                return msg;
            }
            else {
                msg = new JsonMsg() { Flag = false, Message =Common.PubMsg.InValidateData };
                return msg;
            }
            
        }

        private bool SubmitData(Model.PropagateRecord record,out string message) {
            message = string.Empty;
            log4net.ILog log = OnlineHoggeryLog.GetLog("WebServices.PropagateService.SowPropagateAdd");
            try
            {
                BLL.PropagateRecord prBll = new BLL.PropagateRecord();
                bool result= prBll.AddSowPropagateRecord(record, out message);
                Com.HoggeryLog businessLog = new Com.HoggeryLog()
                {
                    HoggeryID = record.HoggeryID,
                    PigId = record.SowID.ToString(),
                    LogType = Com.HoggeryLogType.Propagate,
                    BusinessLogDate = record.PropagateDate,
                    LogComment = message
                };
                if (result)
                {
                    businessLog.Message = "记录分娩成功";
                    log.Info(businessLog);
                }
                else {
                    businessLog.Message = "记录分娩失败";
                    log.Warn(businessLog);
                }

                return result;
            }catch(Exception ex){
                message = Common.PubMsg.ServerError;
                log.Error(new Com.HoggeryLog() { HoggeryID=record.HoggeryID }, ex);
                return false;
            }
        }

    }
}
